// pages/jimeng/jimeng.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title : "",
    content : "",
    key1: "无",
    key2: "无",
    key3: "无",
    isHaipaChecked : false,
    isKaixinChecked: false,
    isShengqiChecked: false,
    isShangxinChecked: false,
    isYihanChecked: false,
    haipaInitImg : "../../images/haipa.png",
    kaixinInitImg: "../../images/kaixin.png",
    shengqiInitImg: "../../images/shengqi.png",
    shangxinInitImg: "../../images/shangxin.png",
    yihanInitImg: "../../images/yihan.png",
    comInitImg: "../../images/complete.png",
    worry : ""
  },

  //标题输入事件
  listenerTitleInput : function(e){
    this.setData({
      title: e.detail.value
    })
  },

  //内容输入事件
  listenerContentInput : function(e){
    this.setData({
      content: e.detail.value
    })
  },

  //关键词输入事件
  listenerKey1Input : function(e){
    this.setData({
      key1: e.detail.value
    })
  },

  listenerKey2Input: function (e) {
    this.setData({
      key2: e.detail.value
    })
  },

  listenerKey3Input: function (e) {
    this.setData({
      key3: e.detail.value
    })
  },

  //困扰输入事件
  listenerWorryInput : function(e){
    this.setData({
      worry: e.detail.value
    })
  },

  //各个心情点击事件处理函数
  //害怕
  bindTerrifyCheck: function(){
    if(!this.data.isHaipaChecked){
      this.setData({
        isHaipaChecked: true,
        haipaInitImg: "../../images/haipa_selected.png"
      })
    }
    else{
      this.setData({
        isHaipaChecked: false,
        haipaInitImg: "../../images/haipa.png"
      })
    }
    //设置其它表情不选中
    this.setData({
      isKaixinChecked: false,
      isShengqiChecked: false,
      isShangxinChecked: false,
      isYihanChecked: false,
      kaixinInitImg: "../../images/kaixin.png",
      shengqiInitImg: "../../images/shengqi.png",
      shangxinInitImg: "../../images/shangxin.png",
      yihanInitImg: "../../images/yihan.png"
    })
  },

  //开心
  bindHappyCheck: function () {
    if (!this.data.isKaixinChecked) {
      this.setData({
        isKaixinChecked: true,
        kaixinInitImg: "../../images/kaixin_selected.png"
      })
    }
    else {
      this.setData({
        isKaixinChecked: false,
        kaixinInitImg: "../../images/kaixin.png"
      })
    }
    //设置其它表情不选中
    this.setData({
      isHaipaChecked: false,
      isShengqiChecked: false,
      isShangxinChecked: false,
      isYihanChecked: false,
      haipaInitImg: "../../images/haipa.png",
      shengqiInitImg: "../../images/shengqi.png",
      shangxinInitImg: "../../images/shangxin.png",
      yihanInitImg: "../../images/yihan.png"
    })
  },

  //生气
  bindAngryCheck: function () {
    if (!this.data.isShengqiChecked) {
      this.setData({
        isShengqiChecked: true,
        shengqiInitImg: "../../images/shengqi_selected.png"
      })
    }
    else {
      this.setData({
        isShengqiChecked: false,
        shengqiInitImg: "../../images/shengqi.png"
      })
    }
    //设置其它表情不选中
    this.setData({
      isHaipaChecked: false,
      isKaixinChecked: false,
      isShangxinChecked: false,
      isYihanChecked: false,
      haipaInitImg: "../../images/haipa.png",
      kaixinInitImg: "../../images/kaixin.png",
      shangxinInitImg: "../../images/shangxin.png",
      yihanInitImg: "../../images/yihan.png"
    })
  },

  //伤心
  bindSadCheck: function () {
    if (!this.data.isShangxinChecked) {
      this.setData({
        isShangxinChecked: true,
        shangxinInitImg: "../../images/shangxin_selected.png"
      })
    }
    else {
      this.setData({
        isShangxinChecked: false,
        shangxinInitImg: "../../images/shangxin.png"
      })
    }
    //设置其它表情不选中
    this.setData({
      isHaipaChecked: false,
      isKaixinChecked: false,
      isShengqiChecked: false,
      isYihanChecked: false,
      haipaInitImg: "../../images/haipa.png",
      kaixinInitImg: "../../images/kaixin.png",
      shengqiInitImg: "../../images/shengqi.png",
      yihanInitImg: "../../images/yihan.png"
    })
  },

  //遗憾
  bindRegretCheck: function () {
    if (!this.data.isYihanChecked) {
      this.setData({
        isYihanChecked: true,
        yihanInitImg: "../../images/yihan_selected.png"
      })
    }
    else {
      this.setData({
        isYihanChecked: false,
        yihanInitImg: "../../images/yihan.png"
      })
    }
    //设置其它表情不选中
    this.setData({
      isHaipaChecked: false,
      isKaixinChecked: false,
      isShengqiChecked: false,
      isShangxinChecked: false,
      haipaInitImg: "../../images/haipa.png",
      kaixinInitImg: "../../images/kaixin.png",
      shengqiInitImg: "../../images/shengqi.png",
      shangxinInitImg: "../../images/shangxin.png"
    })
  },

  //完成记录
  bindComplete: function(e) {
    var formId = e.detail.formId;
    console.log('form发生了submit事件，推送码为：', formId)

    if (this.data.content.length < 10){
      wx.showModal({
        title: '错误说明',
        content: '梦境内容至少需要输入十个字符',
        showCancel: false
      })
    }
    else{
      this.setData({
        comInitImg: "../../images/complete_checked.png"
      })

      //统计表情的点击情况
      var emotions = "[";
      if (this.data.isHaipaChecked) {
        emotions = emotions + "害怕";
        emotions = emotions + ",";
      }
      if (this.data.isKaixinChecked) {
        emotions = emotions + "开心";
        emotions = emotions + ",";
      }
      if (this.data.isShengqiChecked) {
        emotions = emotions + "生气";
        emotions = emotions + ",";
      }
      if (this.data.isShangxinChecked) {
        emotions = emotions + "伤心";
        emotions = emotions + ",";
      }
      if (this.data.isYihanChecked) {
        emotions = emotions + "遗憾";
        emotions = emotions + ",";
      }
      if(emotions.length > 1){
        emotions = emotions.substring(0,emotions.length - 1)
      }
      emotions = emotions + "]";
      console.log("The emotions are " + emotions)

      var that = this;
      var keys = "[" + this.data.key1 + "," + this.data.key2 + "," + this.data.key3 + "]";
      console.log(keys)
      console.log("SessionKey is " + wx.getStorageSync('sessionkey'))
      //存储梦境
      wx.request({
        url: 'https://api.jiemengshi.com/wechat/dream/record/add',
        data: {
          title: that.data.title,
          content: that.data.content,
          keyWord: keys,
          emotion: emotions,
          worry: that.data.worry,
          formId: formId
        },
        header: {
          'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
          //'Cookie': 'JSESSIONID=D930710CA204EF994D87456D0B2EB7F6'
        },
        success: function (res) {
          console.log(res)
          if(res.data.code == 0){
            wx.showToast({
              title: '添加成功',
            })
            wx.navigateBack({
              delta: 1 //返回的页面数
            })
          }
          else{
            wx.showModal({
              title: '记梦失败',
              content: "长按复制输入的文字内容并存储到便签等软件当中，方便后续尝试重新发布！",
              showCancel: false
            })
          }
        }
      })
    }
  },

  onShareAppMessage: function (res) {
    return {
      title: '解梦师',
      path: '/pages/information/information',
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1200
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          icon: 'loading',
          duration: 1200
        })
      }
    }
  },

  onLoad: function (options) {
    //显示转发
    wx.showShareMenu({
      withShareTicket: true
    })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})